using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace MP3Archiver
{
    public partial class Form1 : Form
    {
        Archiver archiver;
        ThreadStart job;
        Thread thread;

        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;

             archiver = new Archiver();

            //add event listner
            archiver.processedFile += ProcessedFile;
            archiver.completed += ArchiveComplete; 
      
        }

     
        private void button1_Click_1(object sender, EventArgs e)
        {
            DialogResult dr = browseFolders1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                foldersList.Items.Add(browseFolders1.SelectedPath);
                groupBox2.Enabled = true;
                button3.Enabled = true;
                
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (foldersList.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a folder to remove", "There was a problem", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                foldersList.Items.RemoveAt(foldersList.SelectedIndex);
            }

            if (foldersList.Items.Count == 0)
            {
                groupBox2.Enabled = false;
                button3.Enabled = false;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DialogResult dr = browseFolders1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                txtDestinationFolder.Text = browseFolders1.SelectedPath;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            resetControls();
            
        }

        protected override void  OnClosing(CancelEventArgs e)
        {
            if (thread != null)
            {
                thread.Abort();
            }

 	         base.OnClosing(e);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (button3.Text == "Stop")
            {
                thread.Abort();
                MessageBox.Show("Process was stopped", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                groupBox1.Enabled = true;
                groupBox2.Enabled = true;
                status.Items.Add("Process aborted by user");
                button3.Text = "Begin Archiving";
                lblFilesToProcess.Text = "0";
                lblProblemFiles.Text = "0";
                lblProcessedFiles.Text = "0";
                status.SelectedIndex = status.Items.Count - 1;
            }
            else
            {
                status.Items.Clear();

                if (txtDestinationFolder.Text == "")
                {
                    MessageBox.Show("Please specify a destination folder before starting", "There was a problem", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                foreach (string dir in foldersList.Items)
                {
                    CountFilesInDirectory(dir);
                }

                groupBox1.Enabled = false;
                groupBox2.Enabled = false;

                button3.Text = "Stop";

                archiver.SetDestinationFolder(txtDestinationFolder.Text);
                archiver.SetRecurse(ckbRecurse.Checked);
               
                if (ckbWMA.Checked == true) { archiver.AddMusicTypeToProcess(Archiver.MusicType.WMA); }
                if (ckbMP3.Checked == true) { archiver.AddMusicTypeToProcess(Archiver.MusicType.MP3); }


                foreach (string folder in foldersList.Items)
                {
                    archiver.AddFolder(folder);
                }

                progress.Step = 1;
                progress.Maximum = int.Parse(lblFilesToProcess.Text);
                progress.Minimum = 0;

                job = new ThreadStart(archiver.Process);
                thread = new Thread(job);
                thread.IsBackground = false;
                thread.Start();
            }

       
        }

        private void CountFiles()
        {
            
        }

        private void CountFilesInDirectory(string dir)
        {

            

            int count = int.Parse(lblFilesToProcess.Text);

            foreach (string file in Directory.GetFiles(dir))
            {
                Archiver.MusicType type = Archiver.MusicFileType(file);

                if (type != Archiver.MusicType.NONMUSICFILE)
                {
                    if ((ckbWMA.Checked == true && type == Archiver.MusicType.WMA) || (ckbMP3.Checked == true && type == Archiver.MusicType.MP3))
                    count++;
                }
            }

            lblFilesToProcess.Text = count.ToString();


            foreach (string childDirectory in Directory.GetDirectories(dir))
            {
                //recurse
                CountFilesInDirectory(childDirectory);
            }
           

        }


        public void ProcessedFile(object sender, ProcessedEventArgs e)
        {
            status.Items.Add("Processed: " + e.GetFileName());
            status.SelectedIndex = status.Items.Count - 1;
            progress.Increment(1);
            lblProcessedFiles.Text = (int.Parse(lblProcessedFiles.Text)+ 1).ToString();
            if (e.isProblem() == true)
            {
                lblProblemFiles.Text = (int.Parse(lblProblemFiles.Text) + 1).ToString();
            }
        }

        public void ArchiveComplete(object sender, EventArgs e)
        {
            if (lblProblemFiles.Text != "0")
            {
                MessageBox.Show("Archiving complete, though there were " + lblProcessedFiles.Text + " files that could not be archived correctly.  They have been added to a problemfiles directory in the root of your destination folder. Please review the log file in the root of the destination folder for detailed information.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Archiving complete", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            resetControls();


        }

        private void resetControls()
        {
            groupBox1.Enabled = true;
            groupBox2.Enabled = false;
            button3.Enabled = false;
            button3.Text = "Begin Archiving";
            foldersList.Items.Clear();
            txtDestinationFolder.Text = string.Empty;
            ckbRecurse.Checked = true;
            progress.Value = 0;
            lblFilesToProcess.Text = "0";
            lblProcessedFiles.Text = "0";
            lblProblemFiles.Text = "0";
        }

        //private void ckbRemoveSource_CheckedChanged(object sender, EventArgs e)
        //{
        //    if (ckbRemoveSource.Checked == true)
        //    {
        //        if (MessageBox.Show("Are you sure you want to remove the source file once the file has been processed?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
        //        {
        //            ckbRemoveSource.Checked = false;

        //        }
        //    }
        //}

     
      
      

  

        

        
    }
}